package com.lzw.security.service.impl;

import com.lzw.security.entity.*;
import com.lzw.security.exception.MysqlUpdateException;
import com.lzw.security.exception.TidcNulException;
import com.lzw.security.mapper.*;
import com.lzw.security.ov.Status;
import com.lzw.security.ov.UserOV;
import com.lzw.security.service.AddService;
import com.lzw.security.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020-3-28 22:15
 * @description：
 * @modified By：
 * @version:
 */
@Slf4j
@Service
public class AddServiceImpl implements AddService {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private SeckillMapper seckillMapper;
    @Autowired
    private DosingMapper dosingMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private AccessoryMapper accessoryMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public UserOV<Integer> addCategory(Category category) throws TidcNulException, MysqlUpdateException {
        UserOV<Integer> userOV = new UserOV<>();
        if(category.getName()!=null){
            userOV.setData(categoryMapper.addCategory(category));
            userOV.getMeta().setStatus(200);
        }else {
            log.debug("想增加类别但是没有输入名字");
            throw new TidcNulException("增加类别必须设定名字");
        }

        return userOV;
    }

    @Override
    public UserOV<Integer> addCommodity(Commodity commodity) throws TidcNulException {
        UserOV<Integer> userOV = new UserOV<>();
        if(commodity.getName()!=null&&commodity.getPrice()!=null){
            userOV.getMeta().setStatus(200);
            userOV.setData(commodityMapper.addCommodity(commodity));
        }else {
            log.debug("想增加商品但是没有输入名字或者价钱");
            throw new TidcNulException("增加商品必须设定名字和价钱");
        }

        return userOV;
    }

    @Override
    public UserOV<Integer> addSecKill(Seckill seckill) throws TidcNulException {
        UserOV<Integer> userOV = new UserOV<>();
        if(seckill.getName()!=null&&seckill.getPrice()!=null){
            userOV.setData(seckillMapper.addSecKill(seckill)).getMeta().setStatus(200);
        }else{
            log.debug("想增加商品但是没有输入名字或者价钱");
            throw new TidcNulException("增加商品必须设定名字和价钱");
        }
        return userOV;
    }

    @Override
    public UserOV<Integer> addDosing(Dosing dosing) throws TidcNulException {
        UserOV<Integer> userOV = new UserOV<>();
        if(dosing.getName()!=null&&dosing.getPrice()!=null){
            userOV.setData(dosingMapper.addDosing(dosing)).getMeta().setStatus(200);
        }else{
            log.debug("想增加配料但是没有输入名字或者价钱或属性");
            throw new TidcNulException("增加配料必须设定名字和价钱或者属性");
        }
        return userOV;
    }

//    @Override
//    public UserOV<Integer> addDosingInCommodity(DosingCommodity dosingCommodity) {
//        UserOV<Integer> userOV = new UserOV<>();
//        userOV.setData(commodityMapper.addDosingInCommodity(dosingCommodity)).getMeta().setStatus(200);
//        return userOV;
//    }

    @Override
    public UserOV<Integer> addCommodityInCart(Cart cart) {
//        String[] array = cart.getDosings().split(",");
//        List list =new ArrayList();
//        for(int i=0;i<array.length;i++){
//            list.add(Integer.parseInt(array[i]));
//        }
        UserOV<Integer> userOV = new UserOV<>();

        userOV.setData(cartMapper.addCart(cart)).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<Integer> addAccessory(Accessory accessory) throws TidcNulException {
        UserOV<Integer> userOV = new UserOV<>();
        if(accessory.getName()!=null&&accessory.getType()!=null){
            userOV.setData(accessoryMapper.addAccessory(accessory)).getMeta().setStatus(200);

        }else{
            throw new TidcNulException("增加附属品必须设定名字和类型或者属性");
        }
        return userOV;
    }

    @Override
    public UserOV<Integer> addAccessoryDosing(AccessoryDosing accessoryDosing) {
        UserOV<Integer> userOV = new UserOV<>();
        userOV.setData(accessoryMapper.addAccessoryDosing(accessoryDosing)).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<Integer> addCommodityAccessory(CommodityAccessory commodityAccessory) {
        UserOV<Integer> userOV = new UserOV<>();
        userOV.setData(commodityMapper.addCommodityAccessory(commodityAccessory)).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<Integer> addCoupon(Coupon coupon) {
        UserOV<Integer> userOV = new UserOV<>();
        userOV.setData(couponMapper.addCoupon(coupon)).getMeta().setStatus(200);
        return userOV;
    }

    @Override
    public UserOV<Integer> addUserCoupon(Coupon coupon) {
        UserOV<Integer> userOV = new UserOV<>();
        userOV.setData(couponMapper.addUserCoupon(coupon)).getMeta().setStatus(200);
        return userOV;
    }
}
